/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.restaurant;

import java.io.File;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.restaurant.Restaurant;
import com.boxin.ims.modules.wechat.service.restaurant.RestaurantService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 酒店Controller
 * @author Jakemanse
 * @version 2013-11-14
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/restaurant")
public class RestaurantController extends BaseController {

	private final Logger logger = Logger.getLogger(RestaurantController.class);
	@Autowired
	private RestaurantService restaurantService;
	
	@ModelAttribute
	public Restaurant get(@RequestParam(required=false) Long id) {
		if (id != null){
			return restaurantService.get(id);
		}else{
			return new Restaurant();
		}
	}
	
	@RequiresPermissions("wechat:restaurant:restaurant:view")
	@RequestMapping(value = {"list", ""})
	public String list(Restaurant restaurant, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			restaurant.setUser(user);
		}
		
        Page<Restaurant> page = restaurantService.find(new Page<Restaurant>(request, response), restaurant); 
        model.addAttribute("page", page);
		return "modules/wechat/restaurant/restaurantList";
	}

	@RequiresPermissions("wechat:restaurant:restaurant:view")
	@RequestMapping(value = "form")
	public String form(Restaurant restaurant, Model model) {
	
		User user = UserUtils.getUser();
		if (restaurant.getUser() == null || restaurant.getUser().getId() <1){
			restaurant.setUser(user);
		}
		
		model.addAttribute("restaurant", restaurant);
		return "modules/wechat/restaurant/restaurantForm";
	}

	@RequiresPermissions("wechat:restaurant:restaurant:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("logoFile") MultipartFile logoFile,@RequestParam("picfile") MultipartFile picfile,@RequestParam("picfile2") MultipartFile picfile2,@RequestParam("picfile3") MultipartFile picfile3,Restaurant restaurant, Model model,  HttpServletRequest request,RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, restaurant)){
			return form(restaurant, model);
		}
		
		//Logo
		if(logoFile != null && logoFile.getOriginalFilename().length()>4){
			String restPath = WechatUtils.getWechatFoodResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(restPath);
			String sname = logoFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_restaurant_logo_"+System.currentTimeMillis()+new Random().nextInt(1000)+fileType;
			File file = new File(restPath+fname);
			logger.debug("保存文件:"+restPath+fname);
			try {
				logoFile.transferTo(file);
				restaurant.setLogo(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+restPath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		//Pic1
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String restPath = WechatUtils.getWechatFoodResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(restPath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_restaurant_"+System.currentTimeMillis()+new Random().nextInt(1000)+fileType;
			File file = new File(restPath+fname);
			logger.debug("保存文件:"+restPath+fname);
			try {
				picfile.transferTo(file);
				restaurant.setPicName(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+restPath+fname);
				e.printStackTrace();
			}
			
		}
		
		//Pic2
		if(picfile2 != null && picfile2.getOriginalFilename().length()>4){
			String restPath = WechatUtils.getWechatFoodResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(restPath);
			String sname = picfile2.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_restaurant_"+System.currentTimeMillis()+new Random().nextInt(1000)+fileType;
			File file = new File(restPath+fname);
			logger.debug("保存文件:"+restPath+fname);
			try {
				picfile2.transferTo(file);
				restaurant.setPicName2(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+restPath+fname);
				e.printStackTrace();
			}
			
		}
		
		//Pic3
		if(picfile3 != null && picfile3.getOriginalFilename().length()>4){
			String restPath = WechatUtils.getWechatFoodResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(restPath);
			String sname = picfile3.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_restaurant_"+System.currentTimeMillis()+new Random().nextInt(1000)+fileType;
			File file = new File(restPath+fname);
			logger.debug("保存文件:"+restPath+fname);
			try {
				picfile3.transferTo(file);
				restaurant.setPicName3(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+restPath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		restaurantService.save(restaurant);
		addMessage(redirectAttributes, "保存'" + restaurant.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/restaurant/?repage";
	}
	
	@RequiresPermissions("wechat:restaurant:restaurant:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		restaurantService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/restaurant/?repage";
	}

}
